package com.teng.smartdrawer;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.teng.smartdrawer.bean.Component;
import com.teng.smartdrawer.bean.Part;
import com.teng.smartdrawer.parser.DataConstants;
import com.teng.smartdrawer.parser.DataParser;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class AddComponentActivity extends AppCompatActivity implements  AdapterView.OnItemSelectedListener, View.OnClickListener {

    private EditText name , alarm , number, des;
    private Spinner spinner;
    private Button add_sure;
    private ArrayList<Part> componentList;
    private ArrayList<String> typeList = new ArrayList<String>();

    private List<Component> addComponentList = new ArrayList<Component>();

    private String type;
    private Component component;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_part_add);

        Intent intent = getIntent();
        componentList = (ArrayList<Part>) intent.getSerializableExtra("componentList");

        ActionBar actionBar = getSupportActionBar();

        //显示返回图表
        actionBar.setDisplayHomeAsUpEnabled(true);

        //设置ActionBar左边默认的图标是否可用
        actionBar.setDisplayUseLogoEnabled(true);

        // 显示窗口标题
        actionBar.setDisplayShowTitleEnabled(true);

        actionBar.setTitle("添加零器件");


        for (int i = 0; i <componentList.size(); i++) {

            typeList.add(componentList.get(i).getT_name());

        }

        name = (EditText) findViewById(R.id.name);
        alarm = (EditText) findViewById(R.id.alarm);
        number = (EditText) findViewById(R.id.number);
        des = (EditText) findViewById(R.id.des);
        add_sure = (Button) findViewById(R.id.add_sure);
        add_sure.setOnClickListener(this);
        spinner = (Spinner) findViewById(R.id.type);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this , android.R.layout.simple_list_item_1 , typeList);

        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(this);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == android.R.id.home)
        {
            // TODO actionBar返回处理

            if (addComponentList.size() > 0) {

//                if (type.equals(component.getM_tid())){

                    Intent intent = new Intent();

                    intent.putExtra("addComponentList" , (Serializable) addComponentList);

                    setResult(RESULT_OK, intent);

//                }

            }

            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);

    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            switch (msg.what){
                case DataConstants.GET_COMPONENT_OPTION_SIGN:

                    component = (Component) msg.obj;

                    Log.e("hehe" , "handler add ----- >> " + component.toString());

                    if (component.getStatus() == 200){
                        addComponentList.add(component);
                        Toast.makeText(AddComponentActivity.this , "添加成功" , Toast.LENGTH_SHORT).show();
                    }

                    break;

                default:

                    break;
            }

            return false;
        }
    });


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Part part = componentList.get(position);
        type = part.getT_id();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_sure:

                String part_name = name.getText().toString();
                String part_alarm = alarm.getText().toString();
                String part_number = number.getText().toString();
                String part_des = des.getText().toString();

                if (part_alarm == null || part_des == null || part_name == null || part_number == null
                        || part_alarm.equals("") || part_des.equals("") || part_name.equals("") || part_number.equals("")){
                    Toast.makeText(AddComponentActivity.this , "信息不完整" , Toast.LENGTH_SHORT).show();
                    return;
                }

                DataParser.addPart(this , handler , type , part_name , part_alarm , part_number , part_des);

                break;

            default:
                break;
        }
    }
}
